package com.lp.RSTX.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.MultipartConfigElement;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;


@Configuration
@EnableSwagger2
@EnableAsync
public class MyConfig {


    private static final Logger log = LogManager.getLogger(MyConfig.class);

    @Bean
    public Docket userApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //.apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .select()
                .build();
    }

    @Bean
    public RequestMappingInfoHandlerMapping requestMapping() {
        return new RequestMappingHandlerMapping();
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
        factory.setMaxFileSize(DataSize.of(10, DataUnit.GIGABYTES));
        /// 设置总上传数据总大小10M
        factory.setMaxRequestSize(DataSize.of(10, DataUnit.GIGABYTES));
        return factory.createMultipartConfig();
    }


    @Bean//application.properties
    public void p() {
        Properties properties = new Properties();
        String s = "/data/application.properties";
        try (InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(s))) {
            properties.load(inputStreamReader);
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        // 获取key对应的value值
    }

    //读取本地的logback-spring.xml
    @Bean
    public void readXml() {
        String path = "/data/logback-spring.xml";
        try {
            File f = new File(path);
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            Element root = doc.getRootElement();

            for (Iterator<Object> i = root.elementIterator("VALUE"); i.hasNext(); ) {
                i.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}